package com.xiaoyun.controller;

import com.xiaoyun.enums.YesOrNo;
import com.xiaoyun.pojo.Carousel;
import com.xiaoyun.pojo.Category;
import com.xiaoyun.service.CarouseService;
import com.xiaoyun.service.CategoryService;
import com.xiaoyun.utils.JsonUtils;
import com.xiaoyun.utils.RedisUtil;
import com.xiaoyun.utils.XYJSONResult;
import com.xiaoyun.vo.CategoryVO;
import com.xiaoyun.vo.NewItemsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api("首页")
@RestController
@RequestMapping("index")
@RequiredArgsConstructor
public class IndexController {

    private final CarouseService carouseService;

    private final CategoryService categoryService;


    @ApiOperation("获取首页轮播图列表")
    @GetMapping("/carousel")
    public XYJSONResult carousel() {
        List<Carousel> list = carouseService.queryAll(YesOrNo.YES.type);
        return XYJSONResult.ok(list);
    }

    @ApiOperation("获取商品一级分类")
    @GetMapping("/cats")
    public XYJSONResult cats() {
        List<Category> list = categoryService.queryAllRootLevelCat();
        return XYJSONResult.ok(list);
    }

    @ApiOperation("获取商品子分类")
    @GetMapping("/subCat/{rootCatId}")
    public XYJSONResult cats(@PathVariable Integer rootCatId) {
        if (rootCatId == null || rootCatId < 1) {
            return XYJSONResult.errorMap("分类不存在");
        }
        List<CategoryVO> list = categoryService.getSubCatList(rootCatId);
        return XYJSONResult.ok(list);
    }

    @ApiOperation("查询每个一级分类下的最新6条商品数据")
    @GetMapping("/sixNewItems/{rootCatId}")
    public XYJSONResult sixNewItems(@PathVariable Integer rootCatId) {
        if (rootCatId == null || rootCatId < 1) {
            return XYJSONResult.errorMap("分类不存在");
        }
        List<NewItemsVO> list = categoryService.getSixNewItemLazy(rootCatId);
        return XYJSONResult.ok(list);
    }
}
